package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.entity.YljaEntity;
import cn.com.css.taxInspection.attention.entity.YljamxEntity;
import cn.com.css.taxInspection.attention.mapper.YljaMapper;
import cn.com.css.taxInspection.attention.service.YljaService;
import cn.com.css.taxInspection.attention.service.YljamxService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.DateUtils;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("YljaService")
public class YljaServiceImpl extends ServiceImpl<YljaMapper, YljaEntity> implements YljaService {

    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private YljamxService yljamxService;


    @Override
    @DS("oracle")
    public IPage<YljaEntity> queryYljaTable(YljaEntity yljaEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前税务机关代码
        String currentSwjgDm = httpSessionService.getCurrentDeptNo();
        String swjgDmTmp = currentSwjgDm.substring(0, 9);
        IPage<YljaEntity> page = new Page<>(yljaEntity.getPage(), yljaEntity.getLimit());
        LambdaQueryWrapper<YljaEntity> queryWrapper = Wrappers.lambdaQuery();
        //案件编号
        if (TsBqUtils.isNotNull(yljaEntity.getJcajbh())) {
            queryWrapper.like(YljaEntity::getJcajbh, yljaEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(yljaEntity.getNsrsbh())) {
            queryWrapper.like(YljaEntity::getNsrsbh, yljaEntity.getNsrsbh());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(yljaEntity.getNsrmc())) {
            queryWrapper.like(YljaEntity::getNsrmc, yljaEntity.getNsrmc());
        }
        //案件状态
        if (TsBqUtils.isNotNull(yljaEntity.getAjjcztDm())) {
            if ("5".equals(yljaEntity.getAjjcztDm())) {
                // 排除以1、2、3、4开头的
                queryWrapper.not(wrapper -> wrapper
                        .likeRight(YljaEntity::getAjjcztDm, "1")
                        .or()
                        .likeRight(YljaEntity::getAjjcztDm, "2")
                        .or()
                        .likeRight(YljaEntity::getAjjcztDm, "3")
                        .or()
                        .likeRight(YljaEntity::getAjjcztDm, "4")
                );
            } else {
                queryWrapper.likeRight(YljaEntity::getAjjcztDm, yljaEntity.getAjjcztDm());
            }
        }
        //检查科室
        String jcbmDm = yljaEntity.getJcbmDm();
        if (TsBqUtils.isNull(jcbmDm) || "qj".equals(jcbmDm)) {
            queryWrapper.like(YljaEntity::getJcbmDm, swjgDmTmp);
        } else if (!"qs".equals(jcbmDm)) {
            queryWrapper.like(YljaEntity::getJcbmDm, jcbmDm);
        }
        //立案日期
        if (TsBqUtils.isNotNull(yljaEntity.getYljalarqq())) {
            queryWrapper.between(YljaEntity::getLarq, sdf.parse(yljaEntity.getYljalarqq()), sdf.parse(yljaEntity.getYljalarqz()));
        }
        queryWrapper.orderByDesc(YljaEntity::getAjdrrq);
        return this.page(page, queryWrapper);
    }

    @Override
    @DS("oracle")
    public List<YljaEntity> exportYljaTable(YljaEntity yljaEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前税务机关代码
        String currentSwjgDm = httpSessionService.getCurrentDeptNo();
        String swjgDmTmp = currentSwjgDm.substring(0, 9);
        LambdaQueryWrapper<YljaEntity> queryWrapper = Wrappers.lambdaQuery();
        //案件编号
        if (TsBqUtils.isNotNull(yljaEntity.getJcajbh())) {
            queryWrapper.like(YljaEntity::getJcajbh, yljaEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(yljaEntity.getNsrsbh())) {
            queryWrapper.like(YljaEntity::getNsrsbh, yljaEntity.getNsrsbh());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(yljaEntity.getNsrmc())) {
            queryWrapper.like(YljaEntity::getNsrmc, yljaEntity.getNsrmc());
        }
        //案件状态
        if (TsBqUtils.isNotNull(yljaEntity.getAjjcztDm())) {
            if ("5".equals(yljaEntity.getAjjcztDm())) {
                // 排除以1、2、3、4开头的
                queryWrapper.not(wrapper -> wrapper
                        .likeRight(YljaEntity::getAjjcztDm, "1")
                        .or()
                        .likeRight(YljaEntity::getAjjcztDm, "2")
                        .or()
                        .likeRight(YljaEntity::getAjjcztDm, "3")
                        .or()
                        .likeRight(YljaEntity::getAjjcztDm, "4")
                );
            } else {
                queryWrapper.likeRight(YljaEntity::getAjjcztDm, yljaEntity.getAjjcztDm());
            }
        }
        //检查科室
        String jcbmDm = yljaEntity.getJcbmDm();
        if (TsBqUtils.isNull(jcbmDm) || "qj".equals(jcbmDm)) {
            queryWrapper.like(YljaEntity::getJcbmDm, swjgDmTmp);
        } else if (!"qs".equals(jcbmDm)) {
            queryWrapper.like(YljaEntity::getJcbmDm, jcbmDm);
        }
        //立案日期
        if (TsBqUtils.isNotNull(yljaEntity.getYljalarqq())) {
            queryWrapper.between(YljaEntity::getLarq, sdf.parse(yljaEntity.getYljalarqq()), sdf.parse(yljaEntity.getYljalarqz()));
        }
        queryWrapper.orderByDesc(YljaEntity::getLarq);
        return this.list(queryWrapper);
    }



}